
<template>
  <div class="app-container">

    <div class="filter-container">
      <el-row :gutter="10">
        <el-col :span="4">
          <el-select v-model="form.uid" clearable>
            <el-option v-for="item in userList" :key="item.uid" :label="item.nick_name" :value="`${item.uid}`" />
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-input
            v-model="form.keyword"
            placeholder="输入关键词搜索"
            class="input-with-select"
            @keypress.enter.native="getList"
          />
        </el-col>
        <el-col :span="4">
          <el-select
            v-model="form.status"
            style="width:100%"
            placeholder="请选择状态"
            @change="getList"
          >
            <el-option
              v-for="(label, key) in appStatus"
              :key="key"
              :label="label"
              :value="key"
            />
          </el-select>
        </el-col>
        <el-col :span="8">
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="getList"
          >搜索</el-button>
          <el-button
            class="filter-item"
            style="margin-left: 10px;"
            icon="el-icon-edit"
            @click="addApp"
          >
            添加
          </el-button>
        </el-col>

      </el-row>
    </div>

    <el-table
      ref="multipleTable"
      v-loading="listLoading"
      :data="list"
      height="calc(100vh - 230px)"
      style="width: 100%;margin-bottom: 20px;"
      size="mini"
      border
      tooltip-effect="dark"
      :header-cell-style="{ background: '#eaf1fd', color: '#333' }"
      row-key="menu_id"
    >

      <el-table-column width="120px" label="app key">
        <template slot-scope="{ row }">
          <CopyItem :value="`${row.app_key}`" />
        </template>
      </el-table-column>

      <el-table-column width="300px" label="app secret">
        <template slot-scope="{ row }">
          <CopyItem :value="`${row.app_secret}`" />
        </template>
      </el-table-column>

      <el-table-column
        label="应用名"
        prop="app_name"
        width="160px"
      />

      <el-table-column
        label="简介"
        prop="desc"
        min-width="300px"
      />

      <el-table-column
        label="状态"
        width="160px"
        fixed="right"
      >
        <template slot-scope="{ row }">
          <el-select
            v-model="row.status"
            placeholder="请选择状态"
          >
            <el-option
              v-for="(label, key) in appStatus"
              :key="key"
              :label="label"
              :value="+key"
            />
          </el-select>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="right"
        width="260"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{ row }">
          <el-button
            type="primary"
            @click="editApp(row)"
          >
            Edit
          </el-button>

          <el-button
            type="primary"
            @click="showDistributeInterface(row)"
          >
            分配接口
          </el-button>

          <el-button
            type="primary"
            @click="showSetInterfacePrice(row)"
          >
            设置费用
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <UpdateApp
      :visible.sync="updateMember.visible"
      :status="updateMember.status"
      :data.sync="updateMember.data"
    />

    <DistributeInterface :visible.sync="distributeInterface.visible" :data="distributeInterface.data" />
    <SetInterfacePrice :visible.sync="setInterfacePrice.visible" :data="setInterfacePrice.data" />

  </div>
</template>

<script>
import { appList } from '@/api/member/app'
import { memberList } from '@/api/member/index'

import CopyItem from '@/components/Template/Common/CopyItem.vue'
import UpdateApp from '@/components/Template/Member/UpdateApp.vue'
import SetInterfacePrice from '@/components/Template/Member/SetInterfacePrice.vue'
import DistributeInterface from '@/components/Template/Member/DistributeInterface.vue'

import appStatus from './appStatus'
import { getQueryString } from '@/utils/util'

export default {
  name: 'Menu',
  components: {
    CopyItem,
    UpdateApp,
    SetInterfacePrice,
    DistributeInterface
  },

  data() {
    return {
      form: {
        uid: '',
        status: '',
        keyword: ''
      },

      listLoading: false,

      userList: [],

      list: [],

      updateMember: {
        visible: false,
        status: 'add',
        data: {}
      },

      distributeInterface: {
        visible: false,
        data: {}
      },

      setInterfacePrice: {
        visible: false,
        data: {}
      },

      appStatus
    }
  },

  async created() {
    await this.getUserList()
    this.getList()
  },

  methods: {
    getList() {
      this.listLoading = true
      appList(this.form).then(res => {
        this.list = res.data.data
        this.listLoading = false
      })
    },

    async getUserList() {
      await memberList({
        keyword: '',
        status: '',
        tel: ''
      }).then(res => {
        this.userList = res.data.data

        const uid = getQueryString('uid')
        if (uid !== null) {
          this.form.uid = uid
        }
      })
    },

    addApp() {
      this.updateMember.status = 'add'
      this.updateMember.visible = true
    },

    editApp(row) {
      this.updateMember.status = 'edit'
      this.updateMember.data = row
      this.updateMember.visible = true
    },

    showDistributeInterface(row) {
      this.distributeInterface.data = row
      this.distributeInterface.visible = true
    },

    showSetInterfacePrice(row) {
      this.setInterfacePrice.data = row
      this.setInterfacePrice.visible = true
    }
  }
}
</script>
